package main

import "fmt"

/*
例子：寻找最长不含有重复字符串的子串     leetcode题目
*/
// abcabcbb -> abc  3       bbbbbb -> b  1        pwwkew -> wke   3
/*
对于每一个字母 x
	lastOccurred[x] 不存在，或者 < start -> 无需任何操作
	lastOccurred[x] >= start -> 更新start
	更行lastOccurred[x] , 更新maxLength
*/

func lengthOfNonRepeatingSubStr(s string) int {
	lastOccured := make(map[byte]int)
	start := 0
	maxLength := 0
	for i, ch := range []byte(s) {
		if lastI, ok := lastOccured[ch]; ok && lastI >= start {
			start = lastOccured[ch] + 1
		}
		if i-start+1 > maxLength {
			maxLength = i - start + 1
		}
		lastOccured[ch] = i
	}
	return maxLength
}

func main() {
	fmt.Println(lengthOfNonRepeatingSubStr("abcabcbb"))
	fmt.Println(lengthOfNonRepeatingSubStr("bbbbbb"))
	fmt.Println(lengthOfNonRepeatingSubStr("pwwkew"))
	fmt.Println(lengthOfNonRepeatingSubStr(""))
	fmt.Println(lengthOfNonRepeatingSubStr("b"))
	fmt.Println(lengthOfNonRepeatingSubStr("zxcvtwetasd"))
}
